
#ifndef FRACTURE_PIECE_H
#define FRACTURE_PIECE_H

#include "Vec4.h"
#include "Aabb.h"
#include "Model.h"
#include "Geometry.h"
#include "GeometryUtils.h"

#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>


class Model;
using namespace std;


struct FracturePiece
{
	// An (unsmoothed) fracture piece is defined just by listing the voxels in it.
	vector<int> m_voxelIndices;
	
	// The mesh associated with each fracture piece is defined by a set of triangles,
	// whose indices index into the lattice/cage.
	
	// Lattice coords of the vertices of this piece
	vector<int> m_vertices;
	
	// Note, after remapping, these triangles index 'locally' into the m_vertices array above, NOT directly into the lattice
	vector<Geometry::Triangle> m_triangles;
	
	// This geometry is the "final result"!
	Geometry m_geometry;
	
	// Adjacency structure for each vertex in fracture piece
	vector<VertexNeighbourhood> m_vertexNeighbourhoods; 
	
	// Create adjacency data structure for this fracture piece's mesh
	void buildVertexNeighbourhoods();
	
	// Apply Laplacian smoothing to this fracture piece (1 iteration), modifying the voxelization lattice.
	void smooth(struct Voxelization& voxelization);
	
	// Create the actual Geometry of this fracture piece
	void finalizeGeometry(Voxelization& voxelization);
	
	// Generate separate geometries for each disconnected chunk of this fracture piece geometry
	void disconnect(vector<Geometry>& finalGeometries);
	
	// Normals and UVs on the boundary between the original mesh surface and the interior need to be repaired to creat "hard" edges with correct UVs.
	static void repairInterfaces(Geometry& geometry);
	
};

#endif // FRACTURE_PIECE_H